<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="店铺名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入店铺名称"  ></a-input>
            </a-form-model-item> 
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所在地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <j-dict-select-tag type="list" v-model="model.area" dictCode="sys_area" placeholder="请选择所在地区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入详细地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openTime">
              <a-input v-model="model.openTime" placeholder="请输入营业时间"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceType">
              <j-dict-select-tag type="list" v-model="model.serviceType" dictCode="service_type" placeholder="请选择服务类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="24"> 
            <a-form-model-item label="优惠措施" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <div>
                <a-button type="primary" @click="handleAddDiscount">添加优惠措施</a-button>
                <div style="display:block;line-height:30px;margin-top: 5px;" v-for="(item,index) in discountArr" :key="index"><span>{{item.name+"：面向"+item.area+item.type+(item.discount==0.0?'免费。':item.discount+"折。")+("享受优待方式："+item.way)+(item.remark?("。备注："+item.remark+"。"):'')}}</span><a-button type="link" @click="()=>{discountArr.splice(index,1)}">删除</a-button></div>
              </div>
            </a-form-model-item> 
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="优惠简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入优惠简介"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="优惠详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detail">
              <j-editor v-model="model.detail" />
            </a-form-model-item>
          </a-col>   -->
          <a-col :span="24"> 
            <a-form-model-item label="优先措施" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <div>
                <a-button type="primary" @click="handleAddPriority">添加优先措施</a-button>
                <div style="display:block;line-height:30px;margin-top: 5px;" v-for="(item,index) in priorityArr" :key="index"><span>{{item.name+"：面向"+item.area+item.type+item.content+("。享受优待方式："+item.way)+(item.remark?("。备注："+item.remark+"。"):'')}}</span><a-button type="link" @click="()=>{priorityArr.splice(index,1)}">删除</a-button></div>
              </div>
            </a-form-model-item> 
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优先简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priorityDesc">
              <a-input v-model="model.priorityDesc" placeholder="请输入优待内容"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="优先详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priorityDetail">
              <j-editor v-model="model.priorityDetail" />
            </a-form-model-item>
          </a-col>  -->
          <a-col :span="24">
            <a-form-model-item label="机构简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="desc">
              <j-editor v-model="model.businessDesc" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageUrl">
              <j-image-upload isMultiple  v-model="model.imageUrl" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageDesc">
              <j-image-upload v-model="model.imageDesc" :isMultiple="true"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="sp_state" placeholder="请选择审核状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <sys-service-discount-modal ref="disCountForm" @ok="disCountOk"></sys-service-discount-modal >
      <sys-service-priority-modal ref="priorityForm" @ok="priorityOk"></sys-service-priority-modal >
    </j-form-container>
  </a-spin>
</template>

<script> 
  import { httpAction, getAction } from '@/api/manage'
  import SysServiceDiscountModal from './SysServiceDiscountModal'
  import SysServicePriorityModal from './SysServicePriorityModal'

  export default {
    name: 'SysServiceForm',
    components: {
      SysServiceDiscountModal,
      SysServicePriorityModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
         discountArr:[],
         priorityArr:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/discount/sysService/add",
          edit: "/discount/sysService/edit",
          queryById: "/discount/sysService/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        // this.edit(this.modelDefault);
        this.model = Object.assign({}, this.modelDefault);
        this.visible = true;
      },
      edit (record) {
        // this.model = Object.assign({}, record);
        // this.visible = true;
        let that = this
        getAction(this.url.queryById,{id:record.id},'get').then((res)=>{
              if(res.success){
                that.model = Object.assign({}, res.result);
                that.discountArr = res.result.discounts
                that.priorityArr = res.result.priorities
                that.visible = true;
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {

            })
      },
      handleAddDiscount(){
        this.$refs.disCountForm.add();
        this.$refs.disCountForm.title = "新增";
        this.$refs.disCountForm.disableSubmit = false;
      },
      handleAddPriority(){
        this.$refs.priorityForm.add();
        this.$refs.priorityForm.title = "新增";
        this.$refs.priorityForm.disableSubmit = false;
      },
      disCountOk(data){
        console.log(data);
        this.discountArr.push(data)
      },
      priorityOk(data){
        console.log(data);
        this.priorityArr.push(data)
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            // this.model.discounts = this.discountArr
            httpAction(httpurl,{...this.model,discounts:this.discountArr,priorities:this.priorityArr},method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>